import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../pages/Home'
import Login from '../pages/Login'
import Register from '../pages/Register'
import Search from '../pages/Search'
import Test from '../pages/Test'

Vue.use(VueRouter)

//专门做增强的函数
function enhance (target,funcName){
	//第一步：备份一下原始的方法
	const origin = target.prototype[funcName]
	//第二步：修改原始的方法
	target.prototype[funcName] = function(location,okCallback,errCallback){
		// 第三步：追加自己的逻辑
		if(okCallback || errCallback){ //如果使用者传递了成功的回调、失败的回调，那么就直接传递就ok了
			return origin.call(this,location,okCallback,errCallback)
		}else{
			return origin.call(this,location).catch(()=>{})
		}
	}
}

//增强VueRouter原型上的push方法
enhance(VueRouter,'push')
//增强VueRouter原型上的replace方法
enhance(VueRouter,'replace')

export default new VueRouter({
	mode:'history',
	routes:[
		{
			path:'/home',
			component:Home
		},
		{
			path:'/login',
			component:Login
		},
		{
			path:'/register',
			component:Register
		},
		{
			path:'/search',
			component:Search
		},
		{
			path:'/test',
			component:Test
		},
		{
			path:'/',
			redirect:'/home'
		}
	]
})
